"use strict";
define(["jquery",""],function(){
    var globalUtil = {
        // baseUrl:"http://127.0.0.1:9000/api/",
        // baseImageUrl:"http://127.0.0.1:9000/",
        baseUrl:"http://121.199.4.216:9000/api/",
        baseImageUrl:"http://121.199.4.216:9000/",
        dataProcess:function(data,success){
            if(data.code == 201){
                Dialog.toast("<div style='color:red;font-size:18px;font-weight:800;'>登录超时,请重新登录<br/>3秒内未自动跳转至登录界面,请点击<a href='/login.html'>重新登录</a></div>", {"intent":"warn"});
                setTimeout(this.goToLoginHtml, 3000)
            }else if(data.code == 1000){
                Dialog.toast("系统异常，请联系管理员", {"intent":"warn"});
            }else if(data.code != 200){
                Dialog.toast("<div style='color:red;font-size:18px;font-weight:800;'>"+data.message+"</div>", {"intent":"warn"});
            }else{
                success(data);
            }
        },
        get:function(url,success,data,error,async){
            if(error==null){
                error=function(){
                    Dialog.toast("网络异常", {"intent":"warn"});
                }
            }
            if(async==null){
                async=true;
            }
            $.ajax({
                url:this.baseUrl+url,
                headers: {
                    'Authorization': this.getToken()
                },
                async:async,
                type:"GET",
                data:data,
                dataType:"JSON",
                success:function(data){
                    globalUtil.dataProcess(data,success);
                },
                error:error
            })
        },
        post:function(url,success,data,error){
            if(error==null){
                error=function(){
                    alert("网络异常")
                }
            }
            $.ajax({
                headers: {
                    'Authorization': this.getToken()
                },
                url:this.baseUrl+url,
                type:"POST",
                contentType:"application/json",
                data:data,
                dataType:"JSON",
                success:function(data){
                    globalUtil.dataProcess(data,success);
                },
                error:error
            })
        },
        delete:function(url,success,data,error){
            if(error==null){
                error=function(){
                    alert("网络异常")
                }
            }
            $.ajax({
                url:this.baseUrl+url,
                headers: {
                    'Authorization': this.getToken()
                },
                type:"DELETE",
                data:data,
                dataType:"JSON",
                success:function(data){
                    globalUtil.dataProess(data,success);
                },
                error:error
            })
        },
        getLeftMenu:function(){
            return JSON.parse(localStorage.getItem("leftMenu"));
        },
        getTopMenu:function(){
            return JSON.parse(localStorage.getItem("topMenu"));
        },
        existLeftMenu:function(){
            if(localStorage.getItem("leftMenu")==null){
                return false;
            }else{
                return true;
            }
        },
        existTopMenu:function(){
            if(localStorage.getItem("topMenu")==null){
                return false;
            }else{
                return true;
            }
        },
        getToken:function () {
            return localStorage.getItem("token")
        },
        getLoginUser:function() {
            return JSON.parse(localStorage.getItem("loginUser"))
        },
        existCurrProject:function(){
            if(localStorage.getItem("project")==null){
                return false;
            }else{
                return true;
            }
        },
        existLoginUser:function(){
            if(localStorage.getItem("loginUser")==null){
                return false;
            }else{
                return true;
            }
        },
        getCurrProject:function() {
            return JSON.parse(localStorage.getItem("project"))
        },

        setCookie:function (name,value){
            var days = 30; 
            var exp = new Date();
            exp.setTime(exp.getTime()+days*24*60*60*1000);    
            document.cookie=name +"="+escape(value) +";expries     ="+exp.toGMTString();         
        },
        getCookie:function(name){
            var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
            if(arr=document.cookie.match(reg))
                return unescape(arr[2]);
            else
                return null;
        },
        goToLoginHtml:function(){
            //跳转登录页面
            window.history.replaceState(null, "", '/login.html');
            location.reload()
        },
        noLoginToLoginHtml:function(){
            if(localStorage.getItem("token")==null){
                window.history.replaceState(null, "", '/login.html');
                location.reload()
            }
        },
        /**
         * 将数据转化成科学计数法
         */
        scientificNotation:function(data){
            var result = {}
            var e = 0;
            while(data>10){
                data = data/10;
                e++;
            }
            result.a = data;
            result.b = e;
            return result;
        }
    }
    return globalUtil;
})